package com.berkeley.db.berkeleydb;

import com.sleepycat.collections.StoredMap;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.CursorConfig;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.LockConflictException;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationResult;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Transaction;
import com.sleepycat.je.TransactionConfig;
import com.sleepycat.je.dbi.Operation;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

@Service
public class CarScoreStore implements  AbstractStore {
	@Autowired
	EntityStore store;

	@Autowired
	Database database;
	public void put(CarScore entity){
		//PrimaryIndex<String, CarScore> carScoreById = store.getPrimaryIndex(String.class, CarScore.class);
		boolean retry = true;
		int retry_count = 0;
		//Transaction txn = store.getEnvironment().beginTransaction(null, null);
		DatabaseEntry dbKey = null;
		DatabaseEntry dbValue = null;
		try {
			dbKey = new DatabaseEntry("1".getBytes("UTF-8"));
			dbValue = new DatabaseEntry(entity.getName().getBytes("UTF-8"), 0,entity.getName().length());

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}


		database.putNoOverwrite(null, dbKey,dbValue);
//			try {
//				try {
//					txn.commit();
//					txn = null;
//				} catch (DatabaseException e) {
//					System.err.println("Error on txn commit: " + e.toString());
//				}
//				retry = false;
//			} catch (LockConflictException de) {
//				System.out.println("BDB: Deadlock");
//
//				if (retry_count < 5) {  // retry if necessary
//					retry = true;
//					retry_count++;
//				} else {
//					System.err.println("BDB: Out of retries[put]. Giving up.");
//					retry = false;
//				}
//			} catch (DatabaseException e) {
//				retry = false;  // abort and don't retry
//				System.err.println("BDB exception: " + e.toString());
//				e.printStackTrace();
//			} finally {
//				if (txn != null) {
//					try {
//						txn.abort();
//					} catch (Exception e) {
//						System.err.println("Error aborting transaction: " + e.toString());
//						e.printStackTrace();
//					}
//				}
//			}

	}



	public Object get(String carId){
		//PrimaryIndex<String, CarScore> carScoreById = store.getPrimaryIndex(String.class, CarScore.class);

		boolean retry = true;
		int retry_count = 0;
		TransactionConfig txnConfig = new TransactionConfig();
//		txnConfig.setReadCommitted(false);
		CarScore carScore = null;


		DatabaseEntry dbKey = null;
		try {
			dbKey = new DatabaseEntry("1".getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		DatabaseEntry value = new DatabaseEntry();
//		while (retry) {
//			Transaction txn = store.getEnvironment().beginTransaction(null, txnConfig);
//			carScore = carScoreById.get(txn, carId, LockMode.READ_COMMITTED);
			database.get(null,dbKey,value,LockMode.DEFAULT);
//			try {
//				try {
//					txn.commit();
//					txn = null;
//				} catch (DatabaseException e) {
//					System.err.println("Error on txn commit: " + e.toString());
//				}
//				retry = false;
//			} catch (LockConflictException de) {
//				System.out.println("BDB: Deadlock");
//				// retry if necessary
//				if (retry_count < 5) {
//					retry = true;
//					retry_count++;
//				} else {
//					System.err.println("BDB: Out of retries[get]. Giving up.");
//					retry = false;
//				}
//			} catch (DatabaseException e) {
//
//				retry = false;   // abort and don't retry
//				System.err.println("BDB exception: " + e.toString());
//				e.printStackTrace();
//			} finally {
//				if (txn != null) {
//					try {
//						txn.abort();
//					} catch (Exception e) {
//						System.err.println("Error aborting transaction: " + e.toString());
//						e.printStackTrace();
//					}
//				}
//			}
//		}
		String result="";
		try {
			result=new String(value.getData(),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return  value.getData();
	}

	public void delete(String carId) {
		//PrimaryIndex<String, CarScore> carScoreById = store.getPrimaryIndex(String.class, CarScore.class);
		boolean retry = true;
		int retry_count = 0;
		DatabaseEntry dbKey = null;
		try {
			dbKey = new DatabaseEntry("1".getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		DatabaseEntry value = new DatabaseEntry();

//			TransactionConfig transactionConfig = new TransactionConfig();
//			transactionConfig.setReadCommitted(true);

			//Transaction txn = store.getEnvironment().beginTransaction(null, null);
			//通过database删除
			OperationStatus operationStatus=database.delete(null,dbKey);

//          通过游标删除
//			CursorConfig safeCursorConfig =new CursorConfig();
//			safeCursorConfig.setReadUncommitted(true);
//			Cursor cursor =database.openCursor(null,safeCursorConfig);
//			//OperationStatus oper =  cursor.getNext(dbKey,value,LockMode.DEFAULT);
//
//			cursor.delete();
//			cursor.close();

//			try {
//				try {
//
//					//database.sync();
//					txn = null;
//				} catch (DatabaseException e) {
//					System.err.println("Error on txn commit: " + e.toString());
//				}
//				retry = false;
//			} catch (LockConflictException de) {
//				System.out.println("BDB: Deadlock");
//
//				if (retry_count < 5) {  // retry if necessary
//					retry = true;
//					retry_count++;
//				} else {
//					System.err.println("BDB: Out of retries[put]. Giving up.");
//					retry = false;
//				}
//			} catch (DatabaseException e) {
//				retry = false;  // abort and don't retry
//				System.err.println("BDB exception: " + e.toString());
//				e.printStackTrace();
//			} finally {
//				if (txn != null) {
//					try {
//						txn.abort();
//					} catch (Exception e) {
//						System.err.println("Error aborting transaction: " + e.toString());
//						e.printStackTrace();
//					}
//				}
//			}
		}

}
